package remy;

import Engine.src.ca.uqac.dim.turtledb.*;
import Engine.src.ca.uqac.dim.turtledb.util.*;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Exemple {
	
    public static void main(String[] args){
        // Query Translator

        String query = "SELECT nom, prenom FROM personne, emploi WHERE id=2;";
        Relation arbre = QueryTranslator.translate(query);

        // Initialisation optimizer

        QueryOptimizer optimizer = new QueryOptimizer();
        optimizer.addSite("Site 1");
        optimizer.addSite("Site 2");
        optimizer.addSite("Site 3");
        optimizer.addSite("Site 4");
        optimizer.setCoutCommunication("Site 1", "Site 2", 10);
        optimizer.setCoutCommunication("Site 1", "Site 3", 15);
        optimizer.setCoutCommunication("Site 1", "Site 4", 2);
        optimizer.setCoutCommunication("Site 2", "Site 3", 12);
        optimizer.setCoutStockageSite("Site 1", 20);
        optimizer.setCoutStockageSite("Site 2", 25);
        optimizer.setCoutStockageSite("Site 3", 22);
        optimizer.setCoutStockageSite("Site 4", 27);
        optimizer.addFragment("F1", "");
        optimizer.addFragment("F2", "");
        optimizer.setAllocationFragmentSite("Site 1", "F2");
        optimizer.setAllocationFragmentSite("Site 2", "F1");


        // Load tables from files

        Relation r_Astronaut = null, r_Mission = null, r_Crew = null, r_Rocket = null;

        try{
            r_Astronaut = XmlQueryParser.parse(FileReadWrite.getFileContents("data/Space/Astronaut.xml"));
            r_Mission = XmlQueryParser.parse(FileReadWrite.getFileContents("data/Space/Mission.xml"));
            r_Crew = XmlQueryParser.parse(FileReadWrite.getFileContents("data/Space/Crew.xml"));
            r_Rocket = XmlQueryParser.parse(FileReadWrite.getFileContents("data/Space/Rocket.xml"));
        }
        catch (FileNotFoundException e){
            System.err.println("File not found");
            System.exit(1);
        }
        catch (IOException e){
            System.err.println("Error reading files");
            System.exit(1);
        }
        catch (XmlQueryParser.ParseException e){
        System.err.println("Error parsing XML files");
        System.exit(1);
        }
        if (r_Astronaut == null || r_Mission == null || r_Crew == null || r_Rocket == null){
            System.err.println("Error reading Space database");
            System.exit(1);
        }

        // Create QueryPlan

        QueryPlan qp = new QueryPlan();

        qp.put("Site 1", r_Astronaut);
        qp.put("Site 2", r_Mission);
        qp.put("Site 3", r_Crew);
        qp.put("Site 4", r_Rocket);

        System.out.println("Coût du plan de requête : "+optimizer.getCost(qp));

    }
	
}